import { ref } from 'vue';

const DELAY = 800;
export interface ILoading {
  show?: boolean;
  type?: 'loading' | 'pay' | 'feedback';
  mask?: boolean;
  text?: string;
  delay?: number;
}
export const loadingConfig = ref<ILoading>({
  show: false,
  type: 'loading',
  mask: true,
  text: '正在加载...',
  delay: DELAY,
});
export const showLoading = (config?: ILoading, callback?: () => void) => {
  if (config) {
    loadingConfig.value = {
      show: true,
      type: config.type || 'loading',
      mask: config.mask || true,
      text: config.text || '正在加载...',
      delay: config.delay || DELAY,
    };
    const tag = setTimeout(() => {
      loadingConfig.value.show = false;
      // eslint-disable-next-line no-unused-expressions
      callback && callback();
      clearTimeout(tag);
    }, DELAY);
  }
};
